package com.gpio;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class MqRecieverServer {
	
	private static Logger logger = LogManager.getLogger(MqRecieverServer.class);
	
	public static void main(String[] args) {
//		logger.info("======================={}mq订阅服务器初始化开始=======================",DateTimeUtil.getCurrentDateTimeStr());
//		try {
//			//等待mq服务器先启动
//			Thread.sleep(60*1000L);
//			ConnectionFactory cf = new ActiveMQConnectionFactory(Config.MQ_USERNAME, Config.MQ_PASSWORD, Config.MQ_URL);
//			Connection conn = cf.createConnection();
//			conn.start();
//			logger.info("======================={}mq订阅服务器连接mq服务器成功=======================",DateTimeUtil.getCurrentDateTimeStr());
//			Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
//			Topic topic = session.createTopic(Config.MQ_TOPIC);
//			MessageConsumer consumer = session.createConsumer(topic);
//			consumer.setMessageListener(new MessageListener() {
//				public void onMessage(Message message) {
//					logger.info("======================={}mq接收到新消息=======================",DateTimeUtil.getCurrentDateTimeStr());
//					TextMessage tm = (TextMessage) message;
//					try {
//						String xmlMsg = tm.getText();
//						logger.info("======================={}消息内容：{}=======================",DateTimeUtil.getCurrentDateTimeStr(),xmlMsg);
//						String result = "";
//						Document document = DocumentHelper.parseText(xmlMsg);
//						Element root = document.getRootElement();
//						Iterator<Element> i = root.elementIterator();
//						List<Integer> faultList = new ArrayList<Integer>();
//						//mq接收到的消息故障设备号
//						int lightNum = 0;
//						Attribute attribute = null;
//						while (i.hasNext()) {
//							Element e = i.next();
//							String name = e.getName();
//							if ("EquipID".equals(name)) {
//								result += "<EquipID>" + e.getStringValue() + "</EquipID>";
//								if (EquipmentConstant.equipmentMap == null) {
//									logger.info("======================={}映射内容为空，请检查 =======================",DateTimeUtil.getCurrentDateTimeStr());
//									break;
//								}
//								logger.info("=======================e为空{} ; e.getStringValue() {}=======================",e == null,e.getStringValue());
//								lightNum = EquipmentConstant.equipmentMap.get(e.getStringValue());
//							} else if ("EquipName".equals(name)) {
//								result += "<EquipName>" + e.getStringValue() + "</EquipName>";
//							} else if ("FaultType1L".equals(name)) {
////								faultList.addAll(getFault1Info(e));
//								attribute = e.attribute("code");
//								if("-1".equals(attribute.getValue())) {
//									faultList.add(1);
//								} else if("2".equals(attribute.getValue())) {
//									faultList.add(2);
//								} else if("3".equals(attribute.getValue())) {
//									faultList.add(4);
//								}
//								
//							}
//						}
//						String faultStr = "<FaultList>";
//						
//						Map<String, Integer> map = null;
//						for (int faultType : faultList) {
//							faultStr += "<FaultType>" + String.valueOf(faultType) + "</FaultType>";
//							//初始化map
//							map = new HashMap<String, Integer>();
//							map.put("lightNum", lightNum);
//							map.put("faultType", faultType);
//							try {
//								GPIOControlThread.queue.put(map);
//								logger.info("======================={}mq消息放入队列成功=======================",DateTimeUtil.getCurrentDateTimeStr());
//							} catch (InterruptedException e) {
//								e.printStackTrace();
//							}
//						}
//						faultStr += "</FaultList>";
//						result += faultStr;
//						System.out.println(result);
//					} catch (JMSException e) {
//						e.printStackTrace();
//					} catch (DocumentException e1) {
//						e1.printStackTrace();
//					}
//				}
//			});
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//		//创建新线程依次处理队列中的故障信息
//		ExecutorService executorService = Executors.newSingleThreadExecutor();
//		executorService.execute(new GPIOControlThread());
//		executorService.shutdown();
	}
}